﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.CrossCutting.Logging;
using Cesave.Infrastructure.CrossCutting.IoC;
using Cesave.DistributedServices.Core.ErrorHandlers;
using Cesave.DistributedServices.PermisosModule.Resources;
using System.ServiceModel;
using Cesave.Domain.PermisosModule.Contracts.Juntas;

namespace Cesave.DistributedServices.PermisosModule
{
	public partial class PermisosModuleService
	{
		#region IPermisosModuleService Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="estadoGuid"></param>
		/// <returns></returns>
		public List<Junta> FindJuntasByEstado(Guid estadoGuid)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				return juntaService.FindJuntasByEstado(estadoGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="estadoGuid"></param>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Junta> FindJuntasByEstadoAndStatus(Guid estadoGuid, bool status = true)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				return juntaService.FindJuntasByEstadoAndStatus(estadoGuid, status);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="distritoGuid"></param>
		/// <returns></returns>
		public List<Junta> FindJuntasByDistrito(Guid distritoGuid)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				return juntaService.FindJuntasByDistrito(distritoGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="distritoGuid"></param>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Junta> FindJuntasByDistritoAndStatus(Guid distritoGuid, bool status = true)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				return juntaService.FindJuntasByDistritoAndStatus(distritoGuid, status);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="juntaGuid"></param>
		/// <returns></returns>
		public Junta FindJuntaByGuid(Guid juntaGuid)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				return juntaService.FindJuntaByGuid(juntaGuid);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="junta"></param>
		public void AddJunta(Junta junta)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				juntaService.AddJunta(junta);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="junta"></param>
		public void ChangeJunta(Junta junta)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				juntaService.ChangeJunta(junta);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="junta"></param>
		public void DeleteJunta(Junta junta)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				juntaService.DeleteJunta(junta);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="junta"></param>
		public void ActivateJunta(Junta junta)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				juntaService.ActivateJunta(junta);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="junta"></param>
		public void DeactivateJunta(Junta junta)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				juntaService.DeactivateJunta(junta);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="municipioGuid"></param>
		/// <returns></returns>
		public List<Junta> FindJuntasByMunicipio(Guid municipioGuid)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				return juntaService.FindJuntasByMunicipio(municipioGuid);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="municipioGuid"></param>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Junta> FindJuntasByMunicipioAndStatus(Guid municipioGuid, bool status = true)
		{
			try
			{
				IJuntaService juntaService = IoCFactory.Resolve<IJuntaService>();
				return juntaService.FindJuntasByMunicipioAndStatus(municipioGuid, status);
			}
			catch (ArgumentNullException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
			catch (InvalidOperationException ex)
			{
				ITraceManager traceManager = IoCFactory.Resolve<ITraceManager>();
				traceManager.TraceError(ex.Message);

				ServiceError detail = ServiceError.Factory(Messages.exception_InvalidArguments);
				throw new FaultException<ServiceError>(detail);
			}
		}

		#endregion
	}
}
